# == Schema Information
#
# Table name: products # 商品
#
#  id                :bigint(8)        not null, primary key
#  name(名称)          :string
#  category_id(分类id) :integer
#  param_keys(参数键名)  :jsonb
#  param_values(参数值) :jsonb
#  price(价格)         :float
#  des(描述)           :string
#  sale_count(销量)    :integer
#  rest_count(库存)    :integer
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#

class Product < ApplicationRecord

  belongs_to :category, optional: true

  def summary_builder
    Jbuilder.new do |json|
      json.(self, :id, :name, :sale_count, :rest_count, :category_id)
      # json.products_count self.products.count
    end.attributes!
  end

  def detail_jbuilder
    Jbuilder.new do |json|
      json.merge! self.summary_builder
      json.category self.category.summary_builder
    end
  end

end
